﻿using System.Text;

namespace Sharp.Portal.AdminLTE.FormField
{
    /// <summary>
    /// 树形下拉菜单
    /// </summary>
    public class TreeListField : ElementField
    {

        /// <summary>
        /// 树形下拉菜单
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="dataSourceName"></param>
        /// <param name="displayField"></param>
        /// <param name="valueField"></param>
        public TreeListField(string fieldName, string dataSourceName, string displayField, string valueField)
        {
            this.FieldName = fieldName;
            this.DataSourceName = dataSourceName;
            this.DisplayField = displayField;
            this.ValueField = valueField;
        }
        /// <summary>
        /// 数据源名称
        /// </summary>
        public string DataSourceName { get; set; }
        /// <summary>
        /// 显示字段名称
        /// </summary>
        public string DisplayField { get; set; }
        /// <summary>
        /// 值字段名称
        /// </summary>
        public string ValueField { get; set; }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="nonFirst"></param>
        /// <returns></returns>
        public override string Build(bool nonFirst = false)
        {
            if (this.IsBrowsing() && !this.KeepingXType)
            {
                return string.Format("@Html.Bootstrap().Lable().NodeId2NodeText(Model.{1}, Convert.ToString(Model.{0}))", this.FieldName, DataSourceName);
            }
            var sb =new StringBuilder();
            sb.AppendLine("<div class=\"input-group\">");
            sb.AppendFormat("@Html.Bootstrap().TextBoxFor(m => m.{0}).Id(\"{1}Text\").Attribute(\"readonly\",\"readonly\")", FieldName, FieldName.Replace(".", "_"));
            sb.AppendFormat("\r\n<span class=\"input-group-addon\"><i class=\"fa fa-check\" id=\"{1}Trigger\"></i></span></div> @Html.HiddenFor(m => m.{0})\r\n<div id=\"{1}MenuContent\" class=\"menuContent\" style=\"display: none; position: absolute; display: none; right: 15px; top: 33px; z-index: 1000;\">\r\n<ul id=\"{1}_Treemenu\" class=\"ztree\" style=\"margin-top: 0; width: 260px;\"></ul></div>\r\n<script type = \"text/javascript\">\r\nvar data = @MvcHtmlString.Create(Model.{2}.ToJson());\r\nvar {1}List = new app.TreeList();\r\n{1}List.init(data.children, \"{1}\", \"@Model.{0}\",\"{3}\",\"{4}\");\r\n</script>", FieldName, FieldName.Replace(".", "_"), DataSourceName, DisplayField, ValueField);
            

            sb.AppendFormat("@Html.Bootstrap().ValidationMessageFor(m => m.{0},\"DropdownTreeList\")", FieldName);

            return sb.ToString();
        }
    }
}